package com.example.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.db.DBManager;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class pieChart_outActivity extends AppCompatActivity {

    //①实例化一个 List  用来保存你的数据，这里是pieEntry
    List<PieEntry> list=new ArrayList<>();

    int year1,month1;
    String out;
    float [] money=new float[24];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.chart_outcome);
        setTitle("PieChartActivity");

        PieChart pie = (PieChart) findViewById(R.id.pie);
        Spinner year_spinner = findViewById(R.id.spinner_month);
        //类型的选择
        Spinner month_spinner = findViewById(R.id.spinner_year);


        year_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                String result = adapterView.getItemAtPosition(position).toString();
                year1=Integer.parseInt(result.split("年")[0]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        month_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                String result = adapterView.getItemAtPosition(position).toString();
                month1=Integer.parseInt(result.toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //②输入数据
        //其中两个数字对应的分别是 X轴Y轴


        money[0]= DBManager.getMoneyBytype(year1,month1,out,"餐饮");
        money[1]=DBManager.getMoneyBytype(year1,month1,out,"购物");
        money[2]=DBManager.getMoneyBytype(year1,month1,out,"日用");
        money[3]=DBManager.getMoneyBytype(year1,month1,out,"交通");
        money[4]=DBManager.getMoneyBytype(year1,month1,out,"水果");
        money[5]=DBManager.getMoneyBytype(year1,month1,out,"零食");
        money[6]=DBManager.getMoneyBytype(year1,month1,out,"运动");
        money[7]=DBManager.getMoneyBytype(year1,month1,out,"娱乐");
        money[8]=DBManager.getMoneyBytype(year1,month1,out,"通讯");
        money[9]=DBManager.getMoneyBytype(year1,month1,out,"服饰");
        money[10]=DBManager.getMoneyBytype(year1,month1,out,"美容");
        money[11]=DBManager.getMoneyBytype(year1,month1,out,"社交");
        money[12]=DBManager.getMoneyBytype(year1,month1,out,"旅行");
        money[13]=DBManager.getMoneyBytype(year1,month1,out,"数码");
        money[14]=DBManager.getMoneyBytype(year1,month1,out,"医疗");
        money[15]=DBManager.getMoneyBytype(year1,month1,out,"学习");
        money[16]=DBManager.getMoneyBytype(year1,month1,out,"宠物");
        money[17]=DBManager.getMoneyBytype(year1,month1,out,"礼金");
        money[18]=DBManager.getMoneyBytype(year1,month1,out,"礼物");
        money[19]=DBManager.getMoneyBytype(year1,month1,out,"书籍");
        money[20]=DBManager.getMoneyBytype(year1,month1,out,"维修");
        money[21]=DBManager.getMoneyBytype(year1,month1,out,"捐赠");
        money[22]=DBManager.getMoneyBytype(year1,month1,out,"快递");
        money[23]=DBManager.getMoneyBytype(year1,month1,out,"设置");

        float total=money[0]+money[1]+money[2]+money[3]+money[4]+money[5]+money[6]+
                money[7]+money[8]+money[9]+money[10]+money[11]+money[12]+
                money[13]+money[14]+money[15]+money[16]+money[17]+money[18]+
                money[19]+money[20]+money[21]+money[22]+money[23];

        list.add(new PieEntry(money[0]/total,"餐饮"));
        list.add(new PieEntry(money[1]/total, "购物"));
        list.add(new PieEntry(money[2]/total, "日用"));
        list.add(new PieEntry(money[3]/total, "交通"));
        list.add(new PieEntry(money[4]/total, "水果"));
        list.add(new PieEntry(money[5]/total, "零食"));
        list.add(new PieEntry(money[6]/total, "运动"));
        list.add(new PieEntry(money[7]/total, "娱乐"));
        list.add(new PieEntry(money[8]/total, "通讯"));
        list.add(new PieEntry(money[9]/total, "服饰"));
        list.add(new PieEntry(money[10]/total, "美容"));
        list.add(new PieEntry(money[11]/total, "社交"));
        list.add(new PieEntry(money[12]/total, "旅行"));
        list.add(new PieEntry(money[13]/total, "数码"));
        list.add(new PieEntry(money[14]/total, "医疗"));
        list.add(new PieEntry(money[15]/total, "学习"));
        list.add(new PieEntry(money[16]/total, "宠物"));
        list.add(new PieEntry(money[17]/total, "礼金"));
        list.add(new PieEntry(money[18]/total, "礼物"));
        list.add(new PieEntry(money[19]/total, "书籍"));
        list.add(new PieEntry(money[20]/total, "维修"));
        list.add(new PieEntry(money[21]/total, "捐赠"));
        list.add(new PieEntry(money[22]/total, "快递"));
        list.add(new PieEntry(money[23]/total, "设置"));




//⑤实例化Dataset

        //list是你这条线的数据  "销售比例" 是你对这个饼的描述
        PieDataSet PieDataSet = new PieDataSet(list, "数据说明");
        PieDataSet.setSliceSpace(0f); //设置饼状Item之间的间隙
        PieDataSet.setColors(getResources().getColor(R.color.teal_200), getResources().getColor(R.color.teal_700),
                getResources().getColor(R.color.blue),getResources().getColor(R.color.gray_111),
                getResources().getColor(R.color.purple_200),
                getResources().getColor(R.color.purple_500),getResources().getColor(R.color.purple_700),
                getResources().getColor(R.color.hehe_blue)); //为DataSet中的数据匹配上颜色集(饼图Item颜色)
        PieData pieData = new PieData(PieDataSet);
        pieData.setDrawValues(true);            //设置是否显示数据实体(百分比，true:以下属性才有意义)
        pieData.setValueTextColor(Color.WHITE);  //设置所有DataSet内数据实体（百分比）的文本颜色
        pieData.setValueTextSize(12f);
        pieData.setValueFormatter(new PercentFormatter());

        pie.setHoleRadius(0);
        pie.setTransparentCircleRadius(0);
        pie.setData(pieData);
        pie.invalidate();
    }
}
